﻿@page "/fileViewers"
@attribute [TabItemOption(Text = "文件预览")]

<a href="https://www.nuget.org/packages/BootstrapBlazor.FileViewer#readme-body-tab"><h4>文件预览 FileViewer</h4></a>


<Tab IsOnlyRenderActiveTab>
    <TabItem Text="Excel">
        <FileViewer Filename=@Filexlsx />
    </TabItem>
    <TabItem Text="Word">
        <FileViewer Filename=@Filedocx />
    </TabItem>
    <TabItem Text="Load">
        <div class="row g-3">
            <div class="col-6 col-sm-4 col-md-3 col-xl-1">
                <Dropdown TValue="string" Items="Items" OnSelectedItemChanged="@ChangeURL" />
            </div>
            <div class="col-6 col-sm-4 col-md-3 col-xl-8">
                <BootstrapInput @bind-Value="@Url" />
            </div>
            <div class="col-6 col-sm-4 col-md-3 col-xl-auto">
                <Button Color="Color.Primary" OnClick="Apply">加载</Button>
            </div>
        </div>

        <FileViewer @ref="fileViewer" Filename=@Url />
    </TabItem>
    <TabItem Text="Files">
        <ListToList StringList="Files" Style="ListStyle.OrderedList" />
    </TabItem>
</Tab>

@code
{
    [NotNull]
    FileViewer? fileViewer { get; set; }

    private string Filedocx { get; set; } = CombineFilename("sample.docx");

    private string Filexlsx { get; set; } = CombineFilename("sample.xlsx");

    private string Url { get; set; } = CombineFilename("sample.docx");

    private static string Dir { get; set; } = (!Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", "_content", "BlazorShared"))) ? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot") : Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "wwwroot", "_content", "BlazorShared");

    private List<string> Files { get; set; } = new List<string>() { Dir };

    private static string CombineFilename(string filename)
    {
        //if (OperatingSystem.IsBrowser())
        filename = Path.Combine(Dir, "samples", filename);
        return filename;
    }

    List<string> FileList = new List<string>
{
        CombineFilename("sample3.xlsx"),
        CombineFilename("sample2.xlsx"),
        CombineFilename("sample.xlsx"),
        CombineFilename("sample2.docx"),
        CombineFilename("sample.docx"),
        "https://blazor.app1.es/samples/sample3.xlsx",
        "https://blazor.app1.es/samples/sample2.xlsx",
        "https://blazor.app1.es/samples/sample.xlsx",
        "https://blazor.app1.es/samples/sample2.docx",
        "https://blazor.app1.es/samples/sample.docx",
        "https://densen.es/sample/sample.docx",
        "https://localhost:5011/_content/BlazorShared/samples/sample.xlsx",
        "https://localhost:5011/_content/BlazorShared/samples/sample.docx",
    };

    List<SelectedItem> Items = new List<SelectedItem>();

    /// <summary>
    /// OnInitialized 方法
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();
        for (int i = 0; i < FileList.Count; i++)
        {
            Items.Add(new SelectedItem { Text = $"File{i}", Value = FileList[i] });
        }
        Url = FileList[0];

        if (Directory.Exists(Dir))
        {
            Directory.GetFiles(Dir, string.Empty, SearchOption.AllDirectories).ToList().ForEach (a=> Files.Add(a.Replace (Dir, "")));
        }
    }

    private async Task ChangeURL(SelectedItem e)
    {
        Url = e.Value;
        StateHasChanged();
        await fileViewer.Reload(e.Value);
    }

    private async Task Apply()
    {
        await fileViewer.Reload(Url);
    } 

}